unit PtAppVersion;

interface

function GetAppVersionStr: string;

implementation

uses
{$IFDEF MSWINDOWS}
  SysUtils;
{$ENDIF}
{$IFDEF MACOS}
  Macapi.CoreFoundation;
{$ENDIF}
{$IFDEF ANDROID}
  Androidapi.JNI.JavaTypes, Androidapi.JNI.App, Androidapi.Helpers,
  Androidapi.JNI.GraphicsContentViewText;
{$ENDIF}
{$IFDEF IOS}
  iOSapi.Foundation, Macapi.ObjectiveC;
{$ENDIF}

function GetAppVersionStr: string;
{$IFDEF MSWINDOWS}
var
  Rec: LongRec;
begin
  Rec := LongRec(GetFileVersion(ParamStr(0)));
  Result := Format('%d.%d', [Rec.Hi, Rec.Lo])
end;
{$ENDIF MSWINDOWS}
{$IFDEF MACOS}
function GetAppVersionStr: string;
var
  CFStr: CFStringRef;
  Range: CFRange;
begin
  CFStr := CFBundleGetValueForInfoDictionaryKey(
    CFBundleGetMainBundle, kCFBundleVersionKey);
  Range.location := 0;
  Range.length := CFStringGetLength(CFStr);
  SetLength(Result, Range.length);
  CFStringGetCharacters(CFStr, Range, PChar(Result));
end;
{$ENDIF}
{$IFDEF ANDROID}
var
  PackageManager: JPackageManager;
  PackageInfo : JPackageInfo;
begin
  PackageManager := SharedActivity.getPackageManager;
  PackageInfo := PackageManager.getPackageInfo(SharedActivityContext.getPackageName(), TJPackageManager.JavaClass.GET_ACTIVITIES);
  Result  := JStringToString(PackageInfo.versionName);
end;
{$ENDIF}
{$IFDEF IOS}
var
  AppKey: Pointer;
  AppBundle: NSBundle;
  BuildStr : NSString;
begin
  AppKey := (NSSTR('CFBundleVersion') as ILocalObject).GetObjectID;
  AppBundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
  BuildStr := TNSString.Wrap(AppBundle.infoDictionary.objectForKey(AppKey));
  Result := UTF8ToString(BuildStr.UTF8String);
end;
{$ENDIF}

end.
